﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using myMvcBlog.Models;
using myMvcBlog.Infrastructure;

namespace myMvcBlog.Controllers
{
    public class ArchiveController : BaseController
    {
        
         //GET: /Archive/

        public ActionResult Index(int year, int month)
        {
            var posts = (from p in Post.All()
                        where p.PostedTime.Year == year && p.PostedTime.Month == month
                        select p).OrderByDescending(p => p.PostedTime);

            int? page = null;

            if (!string.IsNullOrEmpty(Request.QueryString["page"]))
                page = int.Parse(Request.QueryString["page"]);

            ListView<PostView> lvArchivly = new ListView<PostView>();
            lvArchivly.Title = string.Format("归档: {0}年{1}月", year, month);

            var ps = posts.Skip((page ?? 0) * ItemsPerPage).Take(ItemsPerPage);

            lvArchivly.List = ps.ToPostViews();

            PagerView pager = new PagerView();
            pager.ActionName = "Index";
            pager.ControllerName = "Archive";
            pager.CurrentPage = page ?? 0;
            pager.PageCount = (int)Math.Ceiling((double)posts.Count() / ItemsPerPage);

            lvArchivly.Pager = pager;

            return View(lvArchivly);
        }

    }
}
